package com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPoint.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPoint.repository.HddcWyGeologicalsvypointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPoint.repository.HddcWyGeologicalsvypointRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPoint.repository.entity.HddcWyGeologicalsvypointEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPoint.service.HddcWyGeologicalsvypointService;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPoint.viewobjects.HddcWyGeologicalsvypointQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPoint.viewobjects.HddcWyGeologicalsvypointVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-12-01
 */
@Service
public class HddcWyGeologicalsvypointServiceImpl implements HddcWyGeologicalsvypointService {

	@Autowired
    private HddcWyGeologicalsvypointRepository hddcWyGeologicalsvypointRepository;
    @Autowired
    private HddcWyGeologicalsvypointNativeRepository hddcWyGeologicalsvypointNativeRepository;

    @Override
    public JSONObject queryHddcWyGeologicalsvypoints(HddcWyGeologicalsvypointQueryParams queryParams , int curPage,
                                                     int pageSize) {
        Page<HddcWyGeologicalsvypointEntity> hddcWyGeologicalsvypointPage =
                this.hddcWyGeologicalsvypointNativeRepository.queryHddcWyGeologicalsvypoints(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeologicalsvypointPage);
        return jsonObject;
    }


    @Override
    public HddcWyGeologicalsvypointEntity getHddcWyGeologicalsvypoint(String id) {
        HddcWyGeologicalsvypointEntity hddcWyGeologicalsvypoint = this.hddcWyGeologicalsvypointRepository.findById(id).orElse(null);
         return hddcWyGeologicalsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeologicalsvypointEntity saveHddcWyGeologicalsvypoint(HddcWyGeologicalsvypointEntity hddcWyGeologicalsvypoint) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeologicalsvypoint.setId(uuid);
        hddcWyGeologicalsvypoint.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeologicalsvypoint.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        hddcWyGeologicalsvypoint.setIsValid("1");
        this.hddcWyGeologicalsvypointRepository.save(hddcWyGeologicalsvypoint);
        return hddcWyGeologicalsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeologicalsvypointEntity updateHddcWyGeologicalsvypoint(HddcWyGeologicalsvypointEntity hddcWyGeologicalsvypoint) {
        hddcWyGeologicalsvypoint.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeologicalsvypoint.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeologicalsvypointRepository.save(hddcWyGeologicalsvypoint);
        return hddcWyGeologicalsvypoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeologicalsvypoints(List<String> ids) {
        List<HddcWyGeologicalsvypointEntity> hddcWyGeologicalsvypointList = this.hddcWyGeologicalsvypointRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeologicalsvypointList) && hddcWyGeologicalsvypointList.size() > 0) {
            for(HddcWyGeologicalsvypointEntity hddcWyGeologicalsvypoint : hddcWyGeologicalsvypointList) {
//                this.hddcWyGeologicalsvypointRepository.delete(hddcWyGeologicalsvypoint);
                hddcWyGeologicalsvypoint.setIsValid("0");
                hddcWyGeologicalsvypoint.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcWyGeologicalsvypoint.setUpdateTime(new Date());
                this.hddcWyGeologicalsvypointRepository.save(hddcWyGeologicalsvypoint);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeologicalsvypoint(HddcWyGeologicalsvypointQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeologicalsvypointNativeRepository.queryHddcWyGeologicalsvypoint(queryParams);
        return bigInteger;
    }

}
